package com.llsydn.demo.endCall;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;
import com.android.internal.telephony.ITelephony;
import com.llsydn.hello.R;

import java.lang.reflect.Method;

/**
 * @author lilinshen
 * @title 挂断电话的列子
 * @description 请填写相关描述
 * @date 2018/9/29 15:11
 */
public class EndCallActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_end_call);
    }

    /**
     * 挂断电话
     *
     * @param view
     * @throws Exception
     */
    public void endCall(View view) throws Exception {
        //通过反射调用隐藏的API
        Class clazz = Class.forName("android.os.ServiceManager");
        Method method = clazz.getMethod("getService", String.class);
        IBinder iBinder = (IBinder) method.invoke(null, Context.TELEPHONY_SERVICE);
        //得到接口对象
        ITelephony telephony = ITelephony.Stub.asInterface(iBinder);
        //结束通话
        if (ActivityCompat.checkSelfPermission(EndCallActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(EndCallActivity.this, new String[]{Manifest.permission.CALL_PHONE}, 0);
            Toast.makeText(this, "1", Toast.LENGTH_SHORT).show();
        } else {
            if (ActivityCompat.checkSelfPermission(EndCallActivity.this, Manifest.permission.MODIFY_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(EndCallActivity.this, "2申请挂断电话权限", Toast.LENGTH_SHORT).show();
                ActivityCompat.requestPermissions(EndCallActivity.this, new String[]{Manifest.permission.MODIFY_PHONE_STATE}, 0);
            } else {
                Toast.makeText(this, "3", Toast.LENGTH_SHORT).show();
                telephony.endCall();
            }
        }
    }

}
